package com.manli.api.service;

import com.manli.api.pojo.QueryParamsAdver;
import com.manli.api.util.ObjectEmptyUtil;
import org.apache.commons.lang3.StringUtils;
import org.beetl.sql.core.SQLManager;
import org.beetl.sql.core.SQLReady;
import org.springframework.stereotype.Service;
import java.util.Map;
import java.util.List;
import com.manli.api.pojo.Result;
import com.manli.api.util.ResultUtil;
import org.beetl.sql.core.engine.PageQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.manli.api.pojo.QueryParams;
import com.manli.api.dao.AdvertisementDao;
import com.manli.api.dto.Advertisement;


@Service("advertisementService")
public class AdvertisementService {
    @Autowired
    AdvertisementDao advertisementDao;
    @Autowired
    SQLManager sqlManager;


    public Result queryPage(QueryParamsAdver params) {
        /**
         * sql中表名需要修改
         */
        StringBuffer sql = new StringBuffer("select * from advertisement where advertisement_status != -1 ");
        sql.append(StringUtils.isEmpty(params.getAdvertisementName())?"":" and advertisement_name like '%" + params.getAdvertisementName() + "%'");
        sql.append(params.getAdvertisementType() == null?"":" and advertisement_type = " + params.getAdvertisementType() + "");
        sql.append(params.getAdvertisementStatus() == null?"":" and advertisement_status = " + params.getAdvertisementStatus() + "");
        sql.append(" order by advertisement_sort asc");
        SQLReady sqlReady = new SQLReady(sql.toString());

        PageQuery<Advertisement> pageQuery = new PageQuery<Advertisement>(params.getPage(),params.getLimit());
        PageQuery<Advertisement> query =sqlManager.execute(sqlReady,Advertisement.class,pageQuery);
        return ResultUtil.success(query.getList(),query.getTotalRow());
    }

    public Result queryPageForShop(QueryParamsAdver params) {
        /**
         * sql中表名需要修改
         */
        StringBuffer sql = new StringBuffer("select * from advertisement where advertisement_status = 1 ");
        sql.append(" order by advertisement_sort asc");
        SQLReady sqlReady = new SQLReady(sql.toString());
        List<Advertisement> list =sqlManager.execute(sqlReady,Advertisement.class);
        return ResultUtil.success(list);
    }

    
    public Result queryAll() {
        List<Advertisement> list = advertisementDao.all();
        return ResultUtil.success(list);
    }

    
    public Result getById(Integer advertisementId) {
        Advertisement advertisement = advertisementDao.single(advertisementId);
        return ResultUtil.success(advertisement);
    }

    @Transactional
    public Result save(Advertisement advertisement) {
        ObjectEmptyUtil.adverIsEmpty(advertisement);
        advertisementDao.insert(advertisement);
        return ResultUtil.success();
    }

    @Transactional
    public Result updateById(Advertisement advertisement) {
        ObjectEmptyUtil.adverIsEmpty(advertisement);
        advertisementDao.updateTemplateById(advertisement);
        return ResultUtil.success();
    }

    @Transactional
	public Result updateTemplateById(Advertisement advertisement) {
        if(advertisement.getAdvertisementId() == null){
            return ResultUtil.error(-1,"id缺失");
        }
        advertisementDao.executeUpdate("update advertisement set advertisement_status = -1 where advertisement_id = ?",advertisement.getAdvertisementId());
        return ResultUtil.success();
    }


    @Transactional
    public Result removeByIds(List<Integer> ids) {
        for (Integer id:ids) {
            advertisementDao.executeUpdate("update advertisement set advertisement_status = -1 where advertisement_id = ?",id);
        }
        return ResultUtil.success();
    }



}
